﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Reporting.WebForms;
using System.Data;
using System.Data.SqlClient;

namespace Dragon
{
    public partial class wfSalesByProduct : System.Web.UI.Page
    {
        static DataService ds = new DataService();

        private void Page_Load(object sender, EventArgs e)
        {
            List<string> roles = new List<string> { "All", "ASM", "SS" };
            if (Session["Username"] == null || roles.Contains(Session["UserType"].ToString()) == false)
            {
                Response.Redirect("~/wfLogin.aspx");
            }

            if (!IsPostBack)
            {
                this.ReportViewer1.Visible = false;
                this.ReportViewer2.Visible = false;
                this.txtFromDate.Text = "01/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("yyyy");
                this.txtToDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
                this.ddlProvince.SelectedIndex = 0;
            }
        }

        protected void btnRunReport_Click(object sender, EventArgs e)
        {
            DateTime fromDate = new DateTime();
            DateTime toDate = new DateTime();

            try
            {
                fromDate = DateTime.ParseExact(this.txtFromDate.Text, "dd/MM/yyyy", null);
                toDate = DateTime.ParseExact(this.txtToDate.Text, "dd/MM/yyyy", null);
            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Thông báo", "<script>alert('Ngày tháng không hợp lệ!');</script>");
                return;
            }

            string provinceCode = this.ddlProvince.SelectedValue.ToString();
            string provinceName = this.ddlProvince.SelectedItem.ToString();

            string distributorCode, distributorName;
            if (this.ddlDistributor.SelectedIndex != 0)
            {
                distributorCode = this.ddlDistributor.SelectedValue.ToString();
                distributorName = this.ddlDistributor.SelectedItem.ToString();
            }
            else
            {
                distributorCode = "";
                distributorName = "Tất cả";
            }

            string customerCode, customerName;
            if (this.ddlCustomer.Text != "")
            {
                customerCode = this.ddlCustomer.SelectedValue.ToString();
                customerName = this.ddlCustomer.SelectedItem.ToString();
            }
            else
            {
                customerCode = "";
                customerName = "Tất cả";
            }

            int type = this.ddlType.SelectedIndex;
            string typeName = this.ddlType.SelectedValue.ToString();

            string sql = "exec sp_Report_Sales_By_Product @FromDate, @ToDate, @ProvinceCode, @DistributorCode, @CustomerCode, @Type";
            SqlParameter pFromDate = new SqlParameter("@FromDate", fromDate);
            SqlParameter pToDate = new SqlParameter("@ToDate", toDate);
            SqlParameter pProvinceCode = new SqlParameter("@ProvinceCode", provinceCode);
            SqlParameter pDistributorCode = new SqlParameter("@DistributorCode", distributorCode);
            SqlParameter pCustomerCode = new SqlParameter("@CustomerCode", customerCode);
            SqlParameter pType = new SqlParameter("@Type", type);
            ds.SetData(sql, pFromDate, pToDate, pProvinceCode, pDistributorCode, pCustomerCode, pType);

            if (type == 0)
                LoadRevenuesReport(fromDate, toDate, provinceName, distributorName, customerName, typeName);
            else
                LoadUnitSoldReport(fromDate, toDate, provinceName, distributorName, customerName, typeName);
        }      
 
        private void LoadRevenuesReport(DateTime fromDate, DateTime toDate, string provinceName, string distributorName, string customerName, string typeName)
        {
            this.ReportViewer1.Visible = true;
            this.ReportViewer2.Visible = false;
            ReportParameter[] parameters = new ReportParameter[6];
            parameters[0] = new ReportParameter("FromDate", fromDate.ToString("M/dd/yyyy"));
            parameters[1] = new ReportParameter("ToDate", toDate.ToString("M/dd/yyyy"));
            parameters[2] = new ReportParameter("Province", provinceName);
            parameters[3] = new ReportParameter("Distributor", distributorName);
            parameters[4] = new ReportParameter("Customer", customerName);
            parameters[5] = new ReportParameter("Type", typeName);
            this.ReportViewer1.LocalReport.SetParameters(parameters);

            DataTable dt = ds.GetData("select * from ReportSalesByProductRevenues");
            ReportDataSource rds = new ReportDataSource("dsSalesByProductRevenues", dt);
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(rds);
            ReportViewer1.LocalReport.Refresh();
        }

        private void LoadUnitSoldReport(DateTime fromDate, DateTime toDate, string provinceName, string distributorName, string customerName, string typeName)
        {
            this.ReportViewer1.Visible = false;
            this.ReportViewer2.Visible = true;
            ReportParameter[] parameters = new ReportParameter[6];
            parameters[0] = new ReportParameter("FromDate", fromDate.ToString("M/dd/yyyy"));
            parameters[1] = new ReportParameter("ToDate", toDate.ToString("M/dd/yyyy"));
            parameters[2] = new ReportParameter("Province", provinceName);
            parameters[3] = new ReportParameter("Distributor", distributorName);
            parameters[4] = new ReportParameter("Customer", customerName);
            parameters[5] = new ReportParameter("Type", typeName);
            this.ReportViewer2.LocalReport.SetParameters(parameters);

            DataTable dt = ds.GetData("select * from ReportSalesByProductUnitSold");
            ReportDataSource rds = new ReportDataSource("dsSalesByProductUnitSold", dt);
            ReportViewer2.LocalReport.DataSources.Clear();
            ReportViewer2.LocalReport.DataSources.Add(rds);
            ReportViewer2.LocalReport.Refresh();
        }

        protected void CustomerOnDataBound(object sender, EventArgs e)
        {
            this.ddlCustomer.Items.Insert(0, new ListItem("Tất cả", ""));
            this.ddlCustomer.SelectedIndex = 0;
        }        

        protected void DistributorDataBound(object sender, EventArgs e)
        {
            this.ddlDistributor.Items.Insert(0, new ListItem("Tất cả", ""));
            this.ddlDistributor.SelectedIndex = 0;
        }        
    }
}